<template>
  <div>
    <!-- 面包屑导航区 -->
    <bread-nav>
      <span slot="c1">订单管理</span>
      <span slot="c2">订单列表</span>
    </bread-nav>

    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- table 表格区 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="400"
        ></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格(元)"
        ></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger"
              >未付款</el-tag
            >
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column width="170" prop="update_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.update_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showBox"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showProgressBox(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 对话框区 ： 城市选择-->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="50%"
      @close="addressFormClose"
    >
      <el-form
        ref="addressFormRef"
        :model="addressForm"
        label-width="80px"
        :rules="addressRules"
      >
        <el-form-item label="省市区/县" prop="address1" label-width="1">
          <el-cascader
            v-model="addressForm.address1"
            :options="citylist"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="closeDialog">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 对话框区 ： 城市选择-->
    <el-dialog title="物流信息" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressData"
          :key="index"
          color="#0bbd87"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import BreadNav from 'components/content/nav/BreadNav.vue'
import { getOrdersList, getProgressReq } from 'network/order.js'
import citylist from './citylist.js'
export default {
  name: 'Order',
  components: { BreadNav },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      // 订单列表数据
      orderList: [],
      // 点单总条数
      total: 0,
      addressDialogVisible: false,
      progressVisible: false,
      // 地址列表
      addressForm: {
        address1: [],
        address2: '',
      },
      // 城市列表
      citylist,
      // 地址验证规则
      addressRules: {
        address1: [
          { required: true, message: '选择省市区县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      // 物流信息
      progressData: [],
    }
  },
  created() {
    // 获取订单列表
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await getOrdersList(this.queryInfo)
      if (res.meta.status !== 200) {
        this.$message.error('获取订单列表失败！')
        return
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 修改分页函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 修改页码值
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 修改地址函数
    showBox() {
      this.addressDialogVisible = true
    },
    // 对话框确认函数
    closeDialog() {
      this.$refs.addressFormRef.validate((valid) => {
        if (!valid) {
          this.$message.error('请填写必要的表单项！')
          return
        }
        console.log(this.addressForm.address1, this.addressForm.address2)
        this.addressDialogVisible = false
      })
    },
    // 地址对话框关闭函数
    addressFormClose() {
      this.$refs.addressFormRef.resetFields()
    },
    // 查看物流进度按钮
    async showProgressBox(scope) {
      this.progressVisible = true
      const { data: res } = await getProgressReq('1106975712662')
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error('获取物流信息失败！')
        return
      }
      this.progressData = res.data
    },
  },
}
</script>

<style scoped>
</style>